<template>
  <div style="width: 1200px;max-width: 1200px;margin: 0 auto; height: 100vh;">
    <span style="display: flex; justify-content: space-between;padding-top: 10px;">
      <router-link to="/main/renshi/kaoqing/ycchuqin" style="">返回</router-link>
      <router-link to="/main/renshi/kaoqing/ycchuqin" style="">新增记录</router-link>
    </span>
    <h2>异常情况出勤签卡申请单列表</h2>
    <!-- 添加搜索框 -->
    <div class="search-bar">
      <el-input v-model="searchForm.employeeId" placeholder="工号" style="width: 200px; margin-right: 10px;" />
      <el-input v-model="searchForm.name" placeholder="姓名" style="width: 200px; margin-right: 10px;" />
      <el-input v-model="searchForm.team" placeholder="班组" style="width: 200px; margin-right: 10px;" />
      <el-button type="primary" @click="handleSearch">搜索</el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="employeeId" label="工号" width="120"></el-table-column>
      <el-table-column prop="name" label="姓名" width="120"></el-table-column>
      <el-table-column prop="date" label="日期" width="120"></el-table-column>
      <el-table-column prop="team" label="班组" width="120"></el-table-column>
      <el-table-column prop="abnormalSituation" label="异常情况" width="200"></el-table-column>
      <el-table-column prop="tabledBy" label="制表人" width="120"></el-table-column>
      <el-table-column prop="reviewedBy" label="审核人" width="120"></el-table-column>
      <el-table-column prop="approvedBy" label="批准人" width="120"></el-table-column>
      <el-table-column label="操作" width="250" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 详情对话框 -->
    <el-dialog title="异常考勤申请详情" :visible.sync="dialogVisible" width="70%">
      <div v-if="detailData">
        <el-descriptions :column="3" border>
          <el-descriptions-item label="工号">{{ detailData.employeeId }}</el-descriptions-item>
          <el-descriptions-item label="姓名">{{ detailData.name }}</el-descriptions-item>
          <el-descriptions-item label="班组">{{ detailData.team }}</el-descriptions-item>
          <el-descriptions-item label="日期">{{ formatDate(detailData.date) }}</el-descriptions-item>
          <el-descriptions-item label="上午">{{ detailData.morning }}</el-descriptions-item>
          <el-descriptions-item label="下午">{{ detailData.afternoon }}</el-descriptions-item>
          <el-descriptions-item label="加班">{{ detailData.overtime }}</el-descriptions-item>
          <el-descriptions-item label="异常情况">{{ detailData.abnormalSituation }}</el-descriptions-item>
          <el-descriptions-item label="备注">{{ detailData.remark }}</el-descriptions-item>
          <el-descriptions-item label="制表人">{{ detailData.tabledBy }}</el-descriptions-item>
          <el-descriptions-item label="审核人">{{ detailData.reviewedBy }}</el-descriptions-item>
          <el-descriptions-item label="批准人">{{ detailData.approvedBy }}</el-descriptions-item>
        </el-descriptions>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAbnormalAttendanceApplyList, getAbnormalAttendanceApplyDetail, deleteAbnormalAttendanceApply } from '@/api/abnormalAttendanceApply';

export default {
  data() {
    return {
      searchForm: {
        employeeId: '',
        name: '',
        team: ''
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      detailData: null
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      try {
        const response = await getAbnormalAttendanceApplyList({
          current: this.currentPage,
          size: this.pageSize,
          employeeId: this.searchForm.employeeId,
          name: this.searchForm.name,
          team: this.searchForm.team
        });
        this.tableData = response.data.records;
        this.total = response.data.total;
      } catch (error) {
        this.$message.error('获取数据失败：' + error.message);
      }
    },
    async handleDetail(row) {
      try {
        const response = await getAbnormalAttendanceApplyDetail(row.id);
        this.detailData = response.data;
        this.dialogVisible = true;
      } catch (error) {
        this.$message.error('获取详情失败：' + error.message);
      }
    },
    handleEdit(row) {
      this.$router.push(`/main/renshi/kaoqing/ycchuqin?id=${row.id}`);
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该考勤记录？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        await deleteAbnormalAttendanceApply(row.id);
        this.$message.success('删除成功');
        this.fetchData();
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('删除失败：' + error.message);
        }
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    handleSearch() {
      this.currentPage = 1;
      this.fetchData();
    },
    formatDate(date) {
      if (!date) return '';
      return new Date(date).toLocaleDateString();
    }
  }
};
</script>

<style scoped>
* {
  font-family: '宋体';
}

.el-table {
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: right;
}

.search-bar {
  margin: 20px 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>